package com.cskaoyan.controller.auth;

import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.auth.AdminInfoBean;
import com.cskaoyan.model.vo.auth.InfoData;
import com.cskaoyan.model.vo.auth.LoginUserData;
import com.cskaoyan.service.system.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Map;

//shiro整合之后，在做具体的开发
//响应结果都是JSON，Controller组件上我们都用@RestController注解
@RestController
@RequestMapping("admin/auth")
public class AuthController {

    @Autowired
    AdminService adminService;

    /**
     * 如果参数比较少，类型比较简单的话，使用map来接收也可以
     */
    @PostMapping("login")
    public BaseRespVo login(@RequestBody Map map) {

        String username = (String) map.get("username");
        String password = (String) map.get("password");

        Integer status = adminService.selectAdmin(username, password);
        if (status != 0) return BaseRespVo.manual(605, "用户名或者密码错误", null);

        // 首次访问获取sessionId
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.setAttribute("username", username);
        String sessionId = (String) session.getId();

        // 添加到Shiro中用户认证

        try {
            subject.login(new UsernamePasswordToken(username, password));
        } catch (Exception e) {
            return BaseRespVo.manual(605, "其他错误", null);
        }

        // 访问成功
        return succeed(username, sessionId);

    }

    @RequestMapping("info")
    public BaseRespVo info(String token) {

        //开发完shiro之后，再整合
        InfoData infoData = new InfoData();
        infoData.setName("admin123");

        //根据primaryPrincipal做查询
        infoData.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        ArrayList<String> roles = new ArrayList<>();
        roles.add("超级管理员");
        infoData.setRoles(roles);
        ArrayList<String> perms = new ArrayList<>();
        perms.add("*");
        infoData.setPerms(perms);

        return BaseRespVo.ok(infoData);
    }

    @PostMapping("logout")
    public BaseRespVo logout(String token) {

        // 退出登录，清除Session中的信息
        SecurityUtils.getSubject().getSession();

        return BaseRespVo.ok(token);
    }


    private BaseRespVo succeed(String username, String sessionId) {

        LoginUserData loginUserData = new LoginUserData();
        AdminInfoBean adminInfo = new AdminInfoBean();
        adminInfo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        adminInfo.setNickName(username);
        loginUserData.setAdminInfo(adminInfo);
        loginUserData.setToken(sessionId);

        return BaseRespVo.ok(loginUserData);
    }
}
